#!/bin/bash
rpm -ivh xinetd-2.3.15-14.el7.x86_64.rpm
rpm -ivh telnet-server-0.17-65.el7_8.x86_64.rpm
systemctl enable telnet.socket  
systemctl start telnet.socket  
#systemctl enable xinetd  
#systemctl start xinetd


# 1.安装zlib
tar -zxvf zlib-1.2.12.tar.gz
cd zlib-1.2.12
./configure --prefix=/usr/local/zlib
make && make install
cd ..
# 2.安装openssl
tar -zxvf openssl-1.1.1q.tar.gz
cd openssl-1.1.1q
./config --prefix=/usr/local/ssl -d shared
make && make install
cd ..

# 备份当前的openssl
mv /usr/bin/openssl /usr/bin/openssl.bak
mv /usr/include/openssl /usr/include/openssl.bak

# 配置使用新版本
ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/ssl/include/openssl /usr/include/openssl

echo '/usr/local/ssl/lib' >> /etc/ld.so.conf
ldconfig -v
# 注：执行ldconfig -v有些报错，直接忽略即可
# 检查openssl是否升级成功：

# [root@cdhslave1 usr]# openssl version
# OpenSSL 1.1.1q  5 Jul 2022
# 如出现以上信息，表明openssl升级成功


# 3.安装openssh-9
# 备份原 sshd_config文件
mkdir /root/bak/
cp /etc/ssh/sshd_config /roor/bak/
#cp /etc/init.d/sshd /root/bak

# 安装
tar -zxvf openssh-9.0p1.tar.gz
cd openssh-9.0p1

./configure --prefix=/usr/local/openssh9 --with-zlib=/usr/local/zlib --with-ssl-dir=/usr/local/ssl

make && make install
cd ..

#禁用我们之前的openssh服务（这条命令执行完就要小心了，因为openssh服务有保护机制，所以禁止该服务后这个ssh会话不会断开，但如果退出该会话之后就不能再通过ssh连接了）

systemctl disable sshd.service --now

# 备份
mkdir /root/bak/old_sshd
cp -f /usr/sbin/sshd  /root/bak/old_sshd/
cp -f /sbin/sshd  /root/bak/old_sshd/
cp -f /etc/ssh/sshd_config  /root/bak/old_sshd/
cp -f /usr/bin/ssh  /root/bak/old_sshd/
cp -f /usr/bin/ssh-keygen  /root/bak/old_sshd/
cp -f /etc/ssh/ssh_host_ecdsa_key.pub  /root/bak/old_sshd/
cp -f /etc/ssh/ssh_host_dsa_key.pub   /root/bak/old_sshd/
mkdir /root/bak/ssh_key
cp -r /root/.ssh/* /root/bak/ssh_key/

yum -y remove openssh
cp -f /root/update_openssh/openssh-9.0p1/contrib/redhat/sshd.init /etc/init.d/sshd
#cp -f /root/bak/sshd /etc/init.d/sshd
chkconfig --add sshd

#mv /usr/sbin/sshd /usr/sbin/sshd.bak
#mv /sbin/sshd /sbin/sshd.bak
#cp -f /usr/local/openssh9/etc/sshd_config /etc/ssh/sshd_config
cp -f /root/bak/old_sshd/sshd_config /etc/ssh/sshd_config
cp -f /usr/local/openssh9/sbin/sshd /usr/sbin/sshd
cp -f /usr/local/openssh9/bin/ssh /usr/bin/ssh
cp -f /usr/local/openssh9/bin/scp /usr/bin/
cp -f /usr/local/openssh9/bin/sftp /usr/bin/
cp -f /usr/local/openssh9/bin/ssh-keygen /usr/bin/ssh-keygen
cp -f /usr/local/openssh9/etc/ssh_host_ecdsa_key.pub /etc/ssh/ssh_host_ecdsa_key.pub
cp -f /usr/local/openssh9/etc/ssh_host_dsa_key.pub /etc/ssh/
sed -i 's/^#PermitRootLogin.*/PermitRootLogin yes /'  /etc/ssh/sshd_config
systemctl status sshd
/sbin/chkconfig sshd on
sshd -version
systemctl start sshd
systemctl status sshd
chkconfig --list
echo 'done'